const express = require('express')
const router = express.Router()
const passport = require('passport')
const Seller = require('../../models/Seller')
const pagination = require('mongoose-sex-page')

router.get('/seller/list/all', passport.authenticate('jwt', { session: false }), (req, res) => {
  // page 当前页
  Seller.find()
    .then((Seller) => {
      if (!Seller) {
        return res.status(400).json({ status: 400, message: '获取商户列表失败', data: [] })
      }
      const total = Seller.length
      return res.json({ status: 200, message: '请求成功', success: true, data: Seller, total })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '获取失败' })
    })
})

router.get('/seller/list', passport.authenticate('jwt', { session: false }), (req, res) => {
  let SellerFields = {}
  if (req.query.opShopTime) SellerFields.opShopTime = req.query.opShopTime
// 责任人
  if (req.query.seller) SellerFields.seller = req.query.seller
//   商店名字
  if (req.query.shopname) SellerFields.shopname = req.query.shopname
//   营业执照
  if (req.query.sellerImg) SellerFields.sellerImg = req.query.sellerImg
//   地址
  if (req.query.address) SellerFields.address = req.query.address
// 电话号码
  if (req.query.phone) SellerFields.phone = req.query.phone

  // page 当前页
  pagination(Seller)
    .find(SellerFields)
    .page(req.query.pageNum)
    .size(req.query.pageSize)
    .exec()
    .then((Seller) => {
      if (!Seller) {
        return res.status(400).json({ status: 400, message: '获取用户列表失败', data: [] })
      }
      const total = Seller.length
      return res.json({ status: 200, message: '请求成功', success: true, data:Seller, total })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '获取列表失败' })
    })
})

router.post('/seller/add', passport.authenticate('jwt', { session: false }), (req, res) => {
  let SellerFields = {}
  if (req.body.opShopTime) SellerFields.opShopTime = req.body.opShopTime
  if (req.body.seller) SellerFields.seller = req.body.seller
  if (req.body.shopname) SellerFields.shopname = req.body.shopname
  if (req.body.sellerImg) SellerFields.sellerImg = req.body.sellerImg
  if (req.body.address) SellerFields.address = req.body.address
  if(req.body.phone) SellerFields.phone=req.body.phone
  new Seller(SellerFields)
    .save()
    .then(() => {
      return res.json({ status: 200, success: true, message: '添加成功' })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '添加失败!' })
    })
})

router.post('/seller/edit/:id', passport.authenticate('jwt', { session: false }), (req, res) => {
  let SellerField = {}
  if (req.body.opShopTime) SellerField.opShopTime = req.body.opShopTime
  if (req.body.seller) SellerField.seller = req.body.seller
  if (req.body.shopname) SellerField.shopname = req.body.shopname
  if (req.body.sellerImg) SellerField.sellerImg = req.body.sellerImg
  if (req.body.address) SellerField.address = req.body.address
  if (req.body.phone) SellerField.phone = req.body.phone

  Seller.findOneAndUpdate({ _id: req.params.id },{ $set: SellerField },{ new: true })
    .then(() => {
      return res.json({ status: 200, success: true, message: '修改成功' })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '修改失败!' })
    })
})

router.delete('/seller/delete/:id', passport.authenticate('jwt', { session: false }), (req, res) => {
  Seller.findOneAndDelete({ _id: req.params.id })
    .then((SellerField) => {
      
      return res.json({ status: 200, success: true, message: '删除成功' })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '删除失败!' })
    })
})

module.exports = router
